<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- el的栅格系统： 一行分24列 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="bank" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体 -->
        <el-tree :data="list" default-expand-all>
          <template v-slot="scope">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="bank" /><span>{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>操作<i class="el-icon-arrow-down" /></span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hDel(scope.data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog :title=" isEdit ? '编辑部门' : '添加部门'" :visible.sync="showDialog" @close="hDialogClose">
      <depDialog
        ref="deptDialog"
        :pid="curId"
        :is-edit="isEdit"
        :origin-list="originList"
        @close="hClose"
        @success="hSuccess"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { array2Tree } from '@/utils/index'
import depDialog from './depDialog.vue'

export default {
  components: { depDialog },
  // 1. vuex: actions
  // 2. 路由跳转
  // 3. 组件created
  data() {
    return {
      originList: [], // 传递给子组件用来做表单的校验
      isEdit: false, // 当前是添加还是编辑
      curId: '',
      showDialog: false,
      // 依赖一份树形数据
      list: [
        {
          name: '财务部',
          manager: '刘备',
          children: [
            {
              name: '财务核算部',
              manager: '张飞'
            },
            {
              name: '税务核算部',
              manager: '关羽'
            }
          ]
        },
        {
          name: '财务部',
          manager: '刘备',
          children: [
            {
              name: '财务核算部',
              manager: '张飞'
            },
            {
              name: '税务核算部',
              manager: '关羽'
            }
          ]
        }
      ]
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 收到子组件的事件：关闭弹层
    hClose() {
      this.showDialog = false
    },
    // 收到子组件的事件: 操作成功
    hSuccess() {
      // 关闭弹窗
      this.showDialog = false
      // 更新数据
      this.loadDepartments()
    },
    async doDel(id) {
      // 1.调用接口做删除
      await delDepartment(id)
      this.$message.success('删除成功')
      // 2.重发请求
      this.loadDepartments()
    },
    // 删除操作
    hDel(id) {
      // 确认框
      this.$confirm('确认要删除?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 删除操作
        this.doDel(id)
      }).catch(() => {

      })
    },
    // 编辑操作
    hEdit(id) {
      // 显示弹窗
      this.showDialog = true
      // 表单中显示详情数据
      this.curId = id
      // 传递一个标识给子组件 告诉处于编辑状态
      this.isEdit = true
      // 获取子组件的引用
      this.$nextTick(() => {
        console.log('获取子组件的引用', this.$refs.deptDialog)
        console.log('当前的curId', id)
        console.log('获取到子组件中的id -- 从父组件传入', this.$refs.deptDialog.id)
        // 调用子组件
        this.$refs.deptDialog.loadDepartmentById()
      })
    },
    // 添加操作
    hAdd(id) {
      // 获取当前被点击的部门的id
      console.log(id)
      this.curId = id
      // 点击添加部门 显示弹框
      this.showDialog = true
      // 传递一个标识给子组件 告诉处于编辑状态
      this.isEdit = false
    },
    hDialogClose() {
      this.$refs.deptDialog.resetForm()
      console.log('dialog,关闭...')
    },
    async loadDepartments() {
      try {
        const res = await getDepartments()
        res.data.depts.shift()
        // 根据业务的需要，去掉数组第一个元素
        console.log(res.data.depts) // 所有的部门

        // 组装一份简单的列表数据，给子组件用来做表单校验
        this.originList = res.data.depts.map(item => {
          return {
            id: item.id,
            pid: item.pid,
            name: item.name,
            code: item.code
          }
        })
        // 渲染数据
        this.list = array2Tree(res.data.depts, '')
      } catch (err) {
        console.dir(err)
        this.$message.error('获取部门信息失败')
      }
    }
  }
}
</script>
